﻿using Oracle.DataAccess.Client;
using System.Configuration;
using System.Data;
using System.Data.Common;
namespace HBase {
    public class DBUtils {
        static DBUtils() {
            Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
        }
        private static IDbConnection wMSConnection = null;
        public static IDbConnection wMSConn(GKDBType type) {
            if( wMSConnection != null ) {
                return wMSConnection;
            }
            wMSConnection = newWMSConn(type);
            return wMSConnection;
        }
        private static IDbConnection WCSConnection = null;
        public static IDbConnection wCSConn(GKDBType type) {
            if( WCSConnection != null ) {
                return WCSConnection;
            }
            WCSConnection = newWCSConn(type);
            return WCSConnection;
        }
        public static IDbConnection newWMSConn(GKDBType type) {
            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["DB_WMS"];
            return createConnection(type ,connectionStringSettings);
        }
        public static IDbConnection newWCSConn(GKDBType type) {
            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["DB_WMS"];
            IDbConnection conn = createConnection(type ,connectionStringSettings);

            return conn;
        }
        static IDbConnection createConnection(GKDBType type ,ConnectionStringSettings connectionStringSettings) {
            if( type == GKDBType.sqlserver ) {
                IDbConnection connection = DbProviderFactories.GetFactory(connectionStringSettings.ProviderName).CreateConnection();
                connection.ConnectionString = connectionStringSettings.ConnectionString;
                connection.Open();
                return connection;
            }
            if( type == GKDBType.oracle ) {
                return openOrcale(connectionStringSettings);
            }

            if( type == GKDBType.mySql ) {
                return openMysql(connectionStringSettings);
            }
            return null;
        }
        static IDbConnection openOrcale(ConnectionStringSettings settings) {
            var connection = new OracleConnection(settings.ConnectionString);
            connection.Open();
            return connection;
        }
        static IDbConnection openMysql(ConnectionStringSettings settings) {
            var connection = new MySql.Data.MySqlClient.MySqlConnection(settings.ConnectionString);
            connection.Open();
            return connection;
        }
    }
}
